package com.qinxueit.yueshu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qinxueit.yueshu.api.parseHtml;
import com.qinxueit.yueshu.entity.Article;
import com.qinxueit.yueshu.entity.Sys;
import com.qinxueit.yueshu.service.ArticleService;
import com.qinxueit.yueshu.service.SysService;
import com.qinxueit.yueshu.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wangqinxue
 * @since 2021-07-26
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private SysService sysService;

    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer currentPage) {
        Page page = new Page(currentPage, 10);
        IPage iPage = articleService.page(page, new QueryWrapper<Article>().orderByDesc("create_time"));
        return Result.success(iPage);
    }

    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable(name = "id") Long id) {
        Article article = articleService.getById(id);
        return Result.success(article);
    }
    @GetMapping("/getTitles")
    public Result getArticleTitle() throws Exception {
        Sys sys = sysService.getOne(new QueryWrapper<Sys>().eq("sys_key", "title_url"));

        for (int i = 1; i < 2; i++) {
            for (int j = 1; j < 20; j++) {
                String parseUrl = sys.getSysValue() + i + "_" + j;
                System.out.println("执行测试:" + sys.getSysValue());
                Article article = parseHtml.getArticleTitle(parseUrl);
                articleService.save(article);
            }
        }
        return Result.success(200);
    }

}
